package com.eqxiu.iom.msg.api.impl;

import com.eqxiu.common.utils.CollectionUtils;
import com.eqxiu.common.utils.StringUtils;
import com.eqxiu.iom.msg.api.UserActionMsgApi;
import com.eqxiu.iom.msg.dto.msg.ActionMsgTriggerDTO;
import com.eqxiu.iom.msg.service.UserActionMsgService;
import org.apache.dubbo.config.annotation.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @classname: UserActionMsgApiImpl.java
 * @author: th
 * @version: 1.0
 * @description: 用户行为触发消息专用api
 * @createtime: 2019-12-04 16:28
 */
@Service(version = "${dubbo.version.iom.msg}")
public class UserActionMsgApiImpl implements UserActionMsgApi {

    private static final Logger logger = LoggerFactory.getLogger(MsgEventApiImpl.class);

    @Autowired
    UserActionMsgService userActionMsgService;

    @Override
    public void sendActionMsg(ActionMsgTriggerDTO dto) {
        long begin = System.currentTimeMillis();
        // 参数必要性校验
        if (dto == null || CollectionUtils.isEmpty(dto.getUserInfoDTOs())
                || StringUtils.isBlank(dto.getEventCode())
                || StringUtils.isBlank(dto.getProducerCode())) {

            logger.warn("UserActionMsgApiImpl.sendActionMsg入参异常.dto:{}", dto);
            return;
        }

        userActionMsgService.sendUserActionMsg(dto);

        // 总计花费时间
        long total = System.currentTimeMillis() - begin;
        logger.info("UserActionMsgApiImpl.sendActionMsg耗时:{}ms", total);
        if (total > 50) {
            logger.info("UserActionMsgApiImpl.sendActionMsg超时,入参ActionMsgTriggerDTO:{}", dto);
        }
    }
}
